package com.youi.web;

import java.text.DecimalFormat;
import java.text.ParseException;
import java.util.Date;
import java.util.List;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.youi.exception.BusinessException;
import com.youi.manage.HttpRequestManage;
import com.youi.common.DelStatus;
import com.youi.service.LeaderstoreService;
import com.youi.service.StoreService;
import com.youi.service.UserorderService;
import com.youi.entity.Leaderstore;
import com.youi.entity.Userorder;
import com.youi.common.Common;
import com.youi.util.CommonInfoUtil;
import com.youi.util.DateUtil;
import com.youi.util.JsonUtil;
import com.youi.util.ObjectUtil;
import com.youi.util.Page;

/**
* 团长绑定门店信息控制层
* @author Administrator
*/
@Controller
public class LeaderstoreController  extends HttpRequestManage{

		@Autowired
		private LeaderstoreService leaderstoreService;
		@Autowired
		private UserorderService userorderService;
		@Autowired
		private StoreService storeService;

		/**
		 *添加或编辑团长绑定门店信息
		*/
		@ResponseBody
		@RequestMapping("addOrEditLeaderstore")
		public String addOrEditLeaderstore(ModelMap modelMap,HttpServletRequest request)throws BusinessException,ParseException{
			Long id=optLong("id",request);
			Leaderstore leaderstore=null;
			if (id!=null) {
				leaderstore=leaderstoreService.get(id);
			}else {
				leaderstore=new Leaderstore();
				leaderstore.setDelStatus(DelStatus.NODEL);
				leaderstore.setGmtCreated(new Date());
			}
			Long leaderId=optLong("leaderId",request);
			if (leaderId!=null){
				leaderstore.setLeaderId(leaderId);
			}
			Long storeId=optLong("storeId",request);
			if (storeId!=null){
				leaderstore.setStoreId(storeId);
			}
			String phone=optString("phone",request);
			if (phone!=null){
				leaderstore.setPhone(phone);
			}
			Integer status=optInteger("status",request);
			if (status!=null){
				leaderstore.setStatus(status);
			}
			Integer level=optInteger("level",request);
			if (level!=null){
				leaderstore.setLevel(level);
			}
			Integer identity=optInteger("identity",request);
			if (identity!=null){
				leaderstore.setIdentity(identity);
			}
			String authStore=optString("authStore",request);
			if (authStore!=null){
				leaderstore.setAuthStore(authStore);
			}
			String authStoreCode=optString("authStoreCode",request);
			if (authStoreCode!=null){
				leaderstore.setAuthStoreCode(authStoreCode);
			}
			String reason=optString("reason",request);
			if (reason!=null){
				leaderstore.setReason(reason);
			}
			leaderstore.setGmtModify(new Date());
			leaderstoreService.saveOrUpdate(leaderstore);
			return JsonUtil.toJson(CommonInfoUtil.SUCCESS, "操作完成");
		}

		/**
		 * I-41
		 * 批量操作团长信息
		*/
		@ResponseBody
		@RequestMapping("sysUserGetInfo/delLeaderstoreByIds")
		public String delLeaderstoreByIds(ModelMap modelMap,HttpServletRequest request) throws BusinessException{
			String ids=getString("ids",request);
			Integer useStatus=optInteger("useStatus", request);
			List<Long> idsList=ObjectUtil.getIdListByIds(ids);
			if (idsList==null||idsList.size()==0) {
				return JsonUtil.toJson(CommonInfoUtil.PARAMERROR, "ids参数错误");
			}
			Map<String, Object> params=new HashMap<String, Object>();
			params.put(Common.DELSTATUS, DelStatus.NODEL);
			params.put(Common.ID, idsList);
			List<Leaderstore> leaderstoreList= leaderstoreService.getByParams(params);
			if (leaderstoreList==null||leaderstoreList.size()==0) {
				return JsonUtil.toJson(CommonInfoUtil.NULLDATA, "空数据");
			}
			for (Leaderstore leaderstore : leaderstoreList) {
				leaderstore.setUseStatus(useStatus);
			}
			leaderstoreService.saveOrUpdateAll(leaderstoreList);
			return JsonUtil.toJson(CommonInfoUtil.SUCCESS, "操作完成");
		}

		/**
		 * I-39
		 * 查询团长信息列表
		*/
		@ResponseBody
		@RequestMapping("sysUserGetInfo/getLeaderstore")
		public String getLeaderstore(ModelMap modelMap,HttpServletRequest request) throws BusinessException,ParseException{
			Integer pageIndex=optInteger("pageIndex",request,1);
			Integer pageNum=optInteger("pageNum",request,10);
			String des=optString("des",request,Common.DESC);
			String pms=optString("pms",request);
			String name=optString("name",request);
			String phone=optString("phone",request);
			String code=optString("code",request);
			Integer status=optInteger("status",request);
			String gmtRegStart=optString("gmtRegStart",request);
			String gmtRegEnd=optString("gmtRegEnd",request);
			String gmtApplyStart=optString("gmtApplyStart",request);
			String gmtApplyEnd=optString("gmtApplyEnd",request);
			Map<String, Object> prams=new HashMap<String, Object>();
			prams.put(Common.DELSTATUS, DelStatus.NODEL);
			if (status!=null) {
				prams.put(Common.STATUS, status);
			}
			if (code!=null&&code.length()>0) {
				List<Long>storeIdList=storeService.getStoreIdList(code);
				if (storeIdList!=null&&storeIdList.size()>0) {
					prams.put("storeId", storeIdList);
				}
			}
			if (name!=null&&name.length()>0) {
				List<Long> leaderIdList=leaderstoreService.getLeaderIdsByName(name);
				if (leaderIdList!=null&&leaderIdList.size()>0) {
					prams.put("leaderId", leaderIdList);
				}
			}
			Map<String, Object> sortPram=new HashMap<String, Object>();
			sortPram.put(Common.ID,des);
			Map<String, Object> searchPram=null;
			if (phone!=null&&phone.length()>0) {
				searchPram=new HashMap<String, Object>();
				searchPram.put("phone", phone);
			}
			Map<String, Object> startTime=null;
			if(gmtRegStart!=null||gmtApplyStart!=null){
				startTime=new HashMap<String, Object>();
				if (gmtRegStart!=null) {
					startTime.put("gmtReg",DateUtil.simpdfyMdHms.parse(gmtRegStart+" 00:00:00"));
				}	
				if (gmtApplyStart!=null) {
					startTime.put("gmtApply", DateUtil.simpdfyMdHms.parse(gmtApplyStart+" 00:00:00"));
				}
			}
			Map<String, Object> endTime=null;
			if(gmtRegEnd!=null||gmtApplyEnd!=null){
				endTime=new HashMap<String, Object>();
				if (gmtRegEnd!=null) {
					endTime.put("gmtReg",DateUtil.simpdfyMdHms.parse(gmtRegEnd+" 23:59:59"));
				}
				if (gmtApplyEnd!=null) {
					endTime.put("gmtApply", DateUtil.simpdfyMdHms.parse(gmtApplyEnd+" 23:59:59"));
				}
			}
			List<Leaderstore> leaderstoreList=leaderstoreService.getByParamsList(prams, searchPram,sortPram,  (pageIndex-1)*pageNum, pageNum, startTime, endTime);
			if (leaderstoreList==null||leaderstoreList.size()==0) {
				return JsonUtil.toJson(CommonInfoUtil.NULLDATA, "空数据");
			}
			Integer pageCount=leaderstoreService.getCountByParamss(prams, searchPram, startTime, endTime);
			modelMap.put(CommonInfoUtil.PAGECOUNT, Page.getCount(pageCount, pageNum));//pageCount  总页数
			modelMap.put(CommonInfoUtil.PAGEINDEX, pageIndex);			//pageIndex     查询页数
			modelMap.put(CommonInfoUtil.ALLCOUINT,pageCount);
			modelMap.put(CommonInfoUtil.DATA, leaderstoreList);		//jsonObjectList json对象集合
			leaderstoreService.getLeaderstoreMsg(leaderstoreList);
			return JsonUtil.toJson(CommonInfoUtil.SUCCESS, "操作完成",modelMap);
		}

		/**
		 * I-40
		 * 查询团长信息详情
		*/
		@ResponseBody
		@RequestMapping("sysUserGetInfo/getLeaderstoreById")
		public String getLeaderstoreById(ModelMap modelMap,HttpServletRequest request) throws BusinessException{
			Long id=getLong("id",request);
			Leaderstore leaderstore=leaderstoreService.get(id);
			if (leaderstore==null) {
				return JsonUtil.toJson(CommonInfoUtil.NULLDATA, "空数据");
			}
			leaderstoreService.getLeaderstoreMsg(leaderstore);
			return JsonUtil.toJson(CommonInfoUtil.SUCCESS, "操作完成",leaderstore);//jsonObject	json对象
		}

		/**
		 * I-42
		 * 查询团长统计
		 * @param modelMap
		 * @param session
		 * @return
		 */
		@ResponseBody
		@RequestMapping("sysUserGetInfo/getLeaderstoreStat")
		public String getAllLeaderstore(ModelMap modelMap,HttpServletRequest request) throws BusinessException{
			//团长总数
			Map<String, Object> prams=new HashMap<String, Object>();
			prams.put(Common.DELSTATUS, DelStatus.NODEL);
			Integer allCount=leaderstoreService.getCount(prams);
			modelMap.put("allCount", allCount);
			//待审核团长数
			prams=new HashMap<String, Object>();
			prams.put(Common.DELSTATUS, DelStatus.NODEL);
			prams.put(Common.STATUS, 0);			
			Integer applyCount=leaderstoreService.getCount(prams);
			modelMap.put("applyCount", applyCount);
			//团长订单数
			prams=new HashMap<String, Object>();
			prams.put(Common.DELSTATUS, DelStatus.NODEL);
			prams.put("orderType", 1);
			Integer leaderOrderCount=userorderService.getCount(prams);//团长订单数
			Double leaderOrderAmount=0.00;
			List<Userorder> userorderList=userorderService.getByParams(prams);
			if (userorderList!=null&&userorderList.size()>0) {
				for (Userorder userorder : userorderList) {
					leaderOrderAmount+=userorder.getAmount();
				}
			}
			DecimalFormat amount=new DecimalFormat("0.00");
			modelMap.put("leaderOrderCount", leaderOrderCount);
			modelMap.put("leaderOrderAmount",amount.format(leaderOrderAmount));
			prams=new HashMap<String, Object>();
			prams.put(Common.DELSTATUS, DelStatus.NODEL);
			prams.put("orderType", 1);
			prams.put(Common.STATUS, 3);
			Integer leaderComCount=userorderService.getCount(prams);//团长订单数
			Double leaderComAmount=0.00;
			userorderList=userorderService.getByParams(prams);
			if (userorderList!=null&&userorderList.size()>0) {
				for (Userorder userorder : userorderList) {
					leaderComAmount+=userorder.getAmount();
				}
			}
			modelMap.put("leaderComCount", leaderComCount);
			modelMap.put("leaderComAmount", amount.format(leaderComAmount));
			return JsonUtil.toJson(CommonInfoUtil.SUCCESS, "操作完成",modelMap);//jsonObjectList	json对象集合
		}
}
